/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.context.impl;

import java.util.HashMap;
import java.util.Map;

import com.genia.toolbox.web.context.impl.ContextWrapper;
import com.genia.toolbox.web.gwt.basics.bean.FileInformation;
import com.genia.toolbox.web.gwt.basics.context.GwtBasicsContext;

/**
 * implementation of {@link GwtBasicsContext}.
 */
public class GwtBasicsContextImpl
    extends ContextWrapper
    implements GwtBasicsContext
{

  /**
   * the progresses.
   */
  private final Map<Long, Integer> progresses = new HashMap<Long, Integer>();

  /**
   * the {@link FileInformation}s.
   */
  private final Map<Long, FileInformation> fileInformations = new HashMap<Long, FileInformation>();



  /**
   * return the upload progress of the file identified by its identifier.
   * 
   * @param identifier
   *          the identifier of the file being uploaded
   * @return the progress of the upload between 0 and 100 *
   * @see com.genia.toolbox.web.gwt.basics.context.GwtBasicsContext#getProgress(java.lang.Long)
   */
  public Integer getProgress(Long identifier)
  {
    return progresses.get(identifier);
  }



  /**
   * set the upload progress of the file identified by its identifier.
   * 
   * @param identifier
   *          the identifier of the file being uploaded
   * @param progess
   *          the progress of the upload between 0 and 100
   * @see com.genia.toolbox.web.gwt.basics.context.GwtBasicsContext#setProgress(java.lang.Long,
   *      int)
   */
  public void setProgress(Long identifier, int progess)
  {
    progresses.put(identifier, progess);
  }



  /**
   * returns the {@link FileInformation} of the file identified by its
   * identifier.
   * 
   * @param identifier
   *          the identifier of the file being uploaded
   * @return the information relative to the file *
   * @see com.genia.toolbox.web.gwt.basics.context.GwtBasicsContext#getFileInformation(java.lang.Long)
   */
  public FileInformation getFileInformation(Long identifier)
  {
    return fileInformations.get(identifier);
  }



  /**
   * set the {@link FileInformation} of the file identified by its identifier.
   * 
   * @param fileInformation
   *          the information relative to the file *
   * @see com.genia.toolbox.web.gwt.basics.context.GwtBasicsContext#setFileInformation(java.lang.Long,
   *      com.genia.toolbox.web.gwt.basics.bean.FileInformation)
   */
  public void setFileInformation(FileInformation fileInformation)
  {
    fileInformations.put(fileInformation.getGwtFileInformation().getSessionIdentifier(), fileInformation);
  }



  /**
   * retrieve the session identifier of a {@link FileInformation} by its
   * permanent identifier.
   * 
   * @param permanentIdentifier
   *          the permanent identifier of the {@link FileInformation}.
   * @return the session identifier of a {@link FileInformation} by its
   *         permanent identifier
   * @see com.genia.toolbox.web.gwt.basics.context.GwtBasicsContext#getSessionIdentifierForPermanentIdentifier(java.lang.Long)
   */
  public Long getSessionIdentifierForPermanentIdentifier(Long permanentIdentifier)
  {
    if (permanentIdentifier == null) {
      return null;
    }
    for (FileInformation fileInformation : fileInformations.values()) {
      if (permanentIdentifier.equals(fileInformation.getPermanentIdentifier())) {
        return fileInformation.getGwtFileInformation().getSessionIdentifier();
      }
    }
    return null;
  }

}
